﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Utility;
using BLL;

public partial class VehicleUI_Insurance_InsuranceAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataBind();
            Session["InsDetail"] = null;
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        if (!ValidateService.IsNumber(txt_InsFee.Text))
        {
            UtilityService.Alert(this.Page, "保费录入错误!");
            txt_InsFee.Focus();
            return;
        }
        if (!ValidateService.IsNumber(txt_InsQuota.Text))
        {
            UtilityService.Alert(this.Page, "保额录入错误!");
            txt_InsQuota.Focus();
            return;
        }
        
        IList<InsuranceDetail> idList = new List<InsuranceDetail>();
        if (Session["InsDetail"] != null)
        {
            idList = (IList<InsuranceDetail>)Session["InsDetail"];
        }
        InsuranceDetail id = new InsuranceDetail();
        id.InsType = ddl_Type.SelectedValue.ToString();
        id.InsFee = decimal.Parse(txt_InsFee.Text.Trim());
        id.InsQuota = decimal.Parse(txt_InsQuota.Text.Trim());

        InsuranceDetail newID = idList.FirstOrDefault<InsuranceDetail>(X => X.InsType == id.InsType);
        if (newID == null)
        {
            idList.Add(id);
        }
        GridView1.DataSource = idList;
        GridView1.DataBind();

        Session["InsDetail"] = idList;

        txt_InsFee.Text = "";
        txt_InsQuota.Text = "";
        ddl_Type.SelectedIndex = 0;

        if (idList.Count > 0)
        {
            txt_BuyFeeSum.Text = idList.Sum<InsuranceDetail>(X => X.InsFee).ToString();
        }
    }
    protected void btn_Sure_Click(object sender, EventArgs e)
    {
        if (Session["InsDetail"] == null)
        {
            UtilityService.Alert(this.Page, "请先添加要填写的保单！");
            return;
        }


        string _frameNO = txt_FrameNO.Text.Trim();
        bool reExist = new VehicleBLL().Exists(_frameNO);
        if (!reExist)
        {
            UtilityService.Alert(this.Page, "不存在该车辆信息,请重新录入车架号!");
            txt_FrameNO.Focus();
            return;
        }


        Insurance ins = new Insurance();
        ins.FrameNO = txt_FrameNO.Text.Trim();
        ins.BuyFeeSum = decimal.Parse(txt_BuyFeeSum.Text.Trim());
        ins.BuyDate = DateTime.Parse(txt_BuyDate.Text.Trim());
        ins.OutDate = DateTime.Parse(txt_OutDate.Text.Trim());
        ins.BuyChannel = txt_BuyChannel.Text.Trim();
        ins.OrganID = (int)Session["OrganID"];
        ins.InputBy = Session["UserID"].ToString();

        ins.InsDetail = (IList<InsuranceDetail>)Session["InsDetail"];

        int re = new InsuranceBLL().Add(ins);
        if (re > 0)
        {
            string msg = "";
            UtilityService.AlertAndRedirect(this.Page, "添加成功! " + msg, "InsuranceMgr.aspx");
        }
        else
        {
            UtilityService.Alert(this.Page, "添加失败! ");
        }
    }
}
